#include "selfinfowidget.h"
#include "debug.h"
#include <QTimer>
#include <QApplication> // 需要包含 QApplication 头文件
#include <QScreen>      // 需要包含 QScreen 头文件
#include <QPainter>
#include <QPainterPath>
#include <QGridLayout>
#include <QGraphicsDropShadowEffect> // 用于添加阴影效果

#include "model/datacenter.h"
#include "toast.h"
using namespace model;

SelfInfoWidget::SelfInfoWidget(QWidget* parent): QDialog(parent) {
    // 设置对象名，方便QSS定位
    this->setObjectName("SelfInfoDialog");

    // 1. 设置属性和窗口标志
    this->setFixedSize(500, 230); // 适当增大宽度和高度
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->move(QCursor::pos()); // 鼠标位置显示

    // 2. 创建布局管理器
    layout = new QGridLayout();
    layout->setSpacing(5);
    layout->setContentsMargins(10, 10, 5, 5);
    layout->setHorizontalSpacing(10);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    // 3. 定义 QSS 样式
    // 统一的字体家族和基础大小
    QString baseFont = "font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;";
    // Label 样式
    QString labelStyle = QString(R"(
        QLabel {
            %1
            font-size: 15px;
            font-weight: 500;
        }
    )").arg(baseFont);

    // QPushButton 通用样式 (用于修改/提交按钮)
    QString btnStyle = QString(R"(
        QPushButton{
            %1
            font-size: 13px; /* 按钮文字略小，避免抢眼 */
            font-weight: 500;
            color: #555; /* 正常状态文字颜色 */
            border: 1px solid #ccc; /* 细边框 */
            background-color: #e8e8e8; /* 浅灰色背景 */
            border-radius: 6px; /* 较小的圆角，与整体风格协调 */
            padding: 3px 8px; /* 增加内边距，提升点击区域 */
        }
        QPushButton:hover{
            background-color: #e0e0e0; /* 鼠标悬停时略微变深 */
            border-color: #bbb;
        }
        QPushButton:pressed{
            color: #ffffff; /* 点击时文字变白 */
            background-color: #07c160; /* 微信绿 */
            border-color: #07c160;
        }
    )").arg(baseFont);

    // QLineEdit 样式
    QString editStyle = QString(R"(
        QLineEdit {
            %1
            border: 1px solid #e0e0e0; /* 细边框 */
            border-radius: 6px; /* 圆角 */
            padding: 4px 8px; /* 左右内边距 */
            background-color: #ffffff; /* 白色背景 */
            color: #333; /* 文字颜色 */
            font-size: 14px;
        }
        QLineEdit:focus {
            border: 1px solid #07c160; /* 聚焦时边框变绿 */
        }
    )").arg(baseFont);

    // QLineEdit 显示状态的 QLabel 样式 (模拟 QLineEdit 的显示效果)
    QString infoLabelStyle = QString(R"(
        QLabel {
            %1
            font-size: 14px;
            color: #333;
            background-color: rgb(225, 225, 225); /* 与QLineEdit背景色一致 */
            border: 1px solid #f0f0f0; /* 浅边框，与QLineEdit区分 */
            border-radius: 6px;
            padding: 4px 8px; /* 与QLineEdit内边距一致 */
        }
    )").arg(baseFont);

    int rowWidth = 50, rowHeight = 35;
    int btnWidth = 75, btnHeight = 28;

    // 4. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setObjectName("avatarButton");
    avatarBtn->setFixedSize(80, 80);
    avatarBtn->setIconSize(QSize(80, 80));
    avatarBtn->setStyleSheet(R"(
        #avatarButton {
            border: none;
            background-color: transparent;
        }
    )");

    // 5. id 信息 (保持只读，不提供修改)
    // 序号标签
    idTag = new QLabel("序号"); // 直接在构造函数中设置文本
    idTag->setFixedSize( rowWidth, rowHeight);
    idTag->setStyleSheet(labelStyle);

    // 序号值显示
    idLabel = new QLabel();
    idLabel->setFixedHeight(rowHeight);
    idLabel->setStyleSheet(infoLabelStyle); // 使用infoLabelStyle
    idLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); // 允许复制

    // 6. 昵称信息
    nameTag = new QLabel("昵称");
    nameTag->setFixedSize(rowWidth, rowHeight);
    nameTag->setStyleSheet(labelStyle);

    // 创建昵称的 QStackedWidget
    nameStack = new QStackedWidget();
    nameLabel = new QLabel();
    nameLabel->setFixedHeight(rowHeight);
    nameLabel->setStyleSheet(infoLabelStyle);
    nameLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    nameEdit = new QLineEdit();
    nameEdit->setFixedHeight(rowHeight);
    nameEdit->setStyleSheet(editStyle);
    nameStack->addWidget(nameLabel); // 索引 0: 显示
    nameStack->addWidget(nameEdit);  // 索引 1: 编辑
    nameStack->setCurrentIndex(0);   // 初始显示 QLabel


    nameModifyBtn = new QPushButton("修改");
    nameModifyBtn->setFixedSize(btnWidth, btnHeight);
    nameModifyBtn->setStyleSheet(btnStyle);
    nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    nameModifyBtn->setIconSize(QSize(16, 16));
    nameSubmitBtn = new QPushButton("提交");
    nameSubmitBtn->setFixedSize(btnWidth, btnHeight);
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    nameSubmitBtn->setIconSize(QSize(16, 16));
    nameBtnStack = new QStackedWidget();
    nameBtnStack->addWidget(nameModifyBtn); // 索引 0: 修改按钮
    nameBtnStack->addWidget(nameSubmitBtn); // 索引 1: 提交按钮
    nameBtnStack->setCurrentIndex(0);       // 初始显示修改按钮

    // 7. 签名信息
    descTag = new QLabel("签名");
    descTag->setFixedSize(rowWidth, rowHeight);
    descTag->setStyleSheet(labelStyle);

    // 创建签名的 QStackedWidget
    descStack = new QStackedWidget();
    descLabel = new QLabel();
    descLabel->setFixedHeight(rowHeight);
    descLabel->setStyleSheet(infoLabelStyle);
    descLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    descEdit = new QLineEdit();
    descEdit->setFixedHeight(rowHeight);
    descEdit->setStyleSheet(editStyle);
    descEdit->setMaxLength(30);
    descStack->addWidget(descLabel); // 索引 0: 显示
    descStack->addWidget(descEdit);  // 索引 1: 编辑
    descStack->setCurrentIndex(0);   // 初始显示 QLabel

    descModifyBtn = new QPushButton("修改");
    descModifyBtn->setFixedSize(btnWidth, btnHeight);
    descModifyBtn->setStyleSheet(btnStyle);
    descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    descModifyBtn->setIconSize(QSize(16, 16));
    descSubmitBtn = new QPushButton("提交");
    descSubmitBtn->setFixedSize(btnWidth, btnHeight);
    descSubmitBtn->setStyleSheet(btnStyle);
    descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    descSubmitBtn->setIconSize(QSize(16, 16));
    descBtnStack = new QStackedWidget();
    descBtnStack->addWidget(descModifyBtn);
    descBtnStack->addWidget(descSubmitBtn);
    descBtnStack->setCurrentIndex(0);

    // 8. 电话信息
    phoneTag = new QLabel("电话");
    phoneTag->setFixedSize(rowWidth, rowHeight);
    phoneTag->setStyleSheet(labelStyle);

    // 创建电话的 QStackedWidget
    phoneStack = new QStackedWidget();
    phoneLabel = new QLabel();
    phoneLabel->setFixedHeight(rowHeight);
    phoneLabel->setStyleSheet(infoLabelStyle);
    phoneLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    phoneEdit = new QLineEdit();
    phoneEdit->setFixedHeight(rowHeight);
    phoneEdit->setStyleSheet(editStyle);
    phoneStack->addWidget(phoneLabel); // 索引 0: 显示
    phoneStack->addWidget(phoneEdit);  // 索引 1: 编辑
    phoneStack->setCurrentIndex(0);    // 初始显示 QLabel

    phoneModifyBtn = new QPushButton("修改");
    phoneModifyBtn->setFixedSize(btnWidth, btnHeight);
    phoneModifyBtn->setStyleSheet(btnStyle);
    phoneModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    phoneModifyBtn->setIconSize(QSize(16, 16));
    phoneSubmitBtn = new QPushButton("提交");
    phoneSubmitBtn->setFixedSize(btnWidth, btnHeight);
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    phoneSubmitBtn->setIconSize(QSize(16, 16));
    phoneBtnStack = new QStackedWidget();
    phoneBtnStack->addWidget(phoneModifyBtn);
    phoneBtnStack->addWidget(phoneSubmitBtn);
    phoneBtnStack->setCurrentIndex(0);

    // 9. 验证码信息
    // 创建验证码的 QStackedWidget，用于控制整行的显示/隐藏
    verifyStack = new QStackedWidget();
    verifyCodeTag = new QLabel("验证码");
    verifyCodeTag->setFixedSize(rowWidth, rowHeight);
    verifyCodeTag->setStyleSheet(labelStyle);
    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(rowHeight);
    verifyCodeEdit->setStyleSheet(editStyle);
    getVerifyCodeBtn = new QPushButton("获取验证码");
    getVerifyCodeBtn->setStyleSheet(btnStyle);
    getVerifyCodeBtn->setFixedSize(btnWidth + 20, btnHeight);

    // 将验证码控件放入一个 QWidget 容器
    QWidget* verifyWidget = new QWidget();
    QHBoxLayout* verifyLayout = new QHBoxLayout(verifyWidget);
    verifyLayout->setContentsMargins(0, 0, 0, 0);
    verifyLayout->setSpacing(5);
    verifyLayout->addWidget(verifyCodeTag);
    verifyLayout->addWidget(verifyCodeEdit, 1);
    verifyLayout->addWidget(getVerifyCodeBtn);

    // 将容器和一个空 widget 添加到 QStackedWidget
    verifyStack->addWidget(new QWidget()); // 索引 0: 空（隐藏）
    verifyStack->addWidget(verifyWidget);  // 索引 1: 显示验证码行
    verifyStack->setCurrentIndex(0);       // 初始隐藏

    // 10. 添加到布局管理器
    layout->addWidget(avatarBtn, 0, 0, 4, 1, Qt::AlignCenter);

    // 序号
    layout->addWidget(idTag, 0, 1, Qt::AlignVCenter | Qt::AlignRight);
    layout->addWidget(idLabel, 0, 2, 1, 2);

    // 昵称
    layout->addWidget(nameTag, 1, 1, Qt::AlignVCenter | Qt::AlignRight);
    layout->addWidget(nameStack, 1, 2, Qt::AlignVCenter);
    layout->addWidget(nameBtnStack, 1, 3, Qt::AlignVCenter);

    // 签名
    layout->addWidget(descTag, 2, 1, Qt::AlignVCenter | Qt::AlignRight);
    layout->addWidget(descStack, 2, 2, Qt::AlignVCenter);
    layout->addWidget(descBtnStack, 2, 3, Qt::AlignVCenter);

    // 电话
    layout->addWidget(phoneTag, 3, 1, Qt::AlignVCenter | Qt::AlignRight);
    layout->addWidget(phoneStack, 3, 2, Qt::AlignVCenter);
    layout->addWidget(phoneBtnStack, 3, 3, Qt::AlignVCenter);

    // 验证码
    layout->addWidget(verifyStack, 4, 1, 1, 3, Qt::AlignVCenter); // 跨三列

    // 设置列的伸展因子
    layout->setColumnStretch(0, 0);
    layout->setColumnMinimumWidth(0, 80);
    layout->setColumnStretch(1, 0);
    layout->setColumnMinimumWidth(1, 30);
    layout->setColumnStretch(2, 1);
    layout->setColumnStretch(3, 0);
    layout->setColumnMinimumWidth(3, btnWidth);

#if TEST_UI
    QPixmap srcAvatar(":/resource/image/kenan.png");
    QPixmap roundedPixmap = model::getRoundRectPixmap(srcAvatar, QSize(80, 80), 8);
    avatarBtn->setIcon(QIcon(roundedPixmap));

    idLabel->setText("123456789"); // 增加长度，测试显示效果
    nameLabel->setText("张三丰");
    descLabel->setText("从今天开始认真敲代码!"); // 较长的签名
    phoneLabel->setText("186 1234 5678");
#endif

    // 11. 加载 信号槽 和 用户消息
    initSingalSlot();
    DataCenter* dataCenter = DataCenter::getInstance();
    UserInfo* myself = dataCenter->getMyself();
    if (myself != nullptr) {
        // 就把个人信息, 显示到界面上
        avatarBtn->setIcon(myself->avatar);
        idLabel->setText(myself->userId);
        nameLabel->setText(myself->nickname);
        descLabel->setText(myself->description);
        phoneLabel->setText(myself->phone);
    }
}


void SelfInfoWidget::initSingalSlot()
{
    connect(nameModifyBtn, &QPushButton::clicked, this, [this]() {
        nameBtnStack->setCurrentIndex(1); // 切换到提交按钮
        nameStack->setCurrentIndex(1);    // 切换到编辑框
        nameEdit->setFocus();
        nameEdit->setText(nameLabel->text());
    });
    // connect(nameSubmitBtn, &QPushButton::clicked, this, [this]() {
    //     nameLabel->setText(nameEdit->text());
    //     nameStack->setCurrentIndex(0); // 切换回显示
    //     nameBtnStack->setCurrentIndex(0); // 切换回修改按钮
    // });
    connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);

    // 签名修改
    connect(descModifyBtn, &QPushButton::clicked, this, [this]() {
        descBtnStack->setCurrentIndex(1);
        descStack->setCurrentIndex(1);
        descEdit->setFocus();
        descEdit->setText(descLabel->text());
    });
    // connect(descSubmitBtn, &QPushButton::clicked, this, [this]() {
    //     descLabel->setText(descEdit->text());
    //     descStack->setCurrentIndex(0);
    //     descBtnStack->setCurrentIndex(0);
    // });
    connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn);

    // 电话修改
    connect(phoneModifyBtn, &QPushButton::clicked, this, [this]() {
        phoneBtnStack->setCurrentIndex(1);
        phoneStack->setCurrentIndex(1);
        phoneEdit->setFocus();
        phoneEdit->setText(phoneLabel->text());
        verifyStack->setCurrentIndex(1); // 显示验证码行
    });
    // connect(phoneSubmitBtn, &QPushButton::clicked, this, [this]() {
    //     phoneLabel->setText(phoneEdit->text());
    //     phoneStack->setCurrentIndex(0);
    //     phoneBtnStack->setCurrentIndex(0);
    //     verifyStack->setCurrentIndex(0); // 隐藏验证码行
    // });
    connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn);
    connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn);

    // 头像点击
    connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);
}

/// 修改昵称
void SelfInfoWidget::clickNameSubmitBtn(){
    // 1. 从输入框获取修改后昵称
    const QString& nickname = nameEdit->text();
    if(nickname.isEmpty()){
        return;
    }
    // 2. 发送网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::changeNicknameDone, this, &SelfInfoWidget::clickNameSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changeNicknameAsync(nickname);
}
void SelfInfoWidget::clickNameSubmitBtnDone(){
    nameLabel->setText(nameEdit->text());
    nameStack->setCurrentIndex(0); // 切换回显示
    nameBtnStack->setCurrentIndex(0); // 切换回修改按钮
}

/// 修改签名
void SelfInfoWidget::clickDescSubmitBtn(){
    // 1. 从输入框获取修改后昵称
    const QString& desc = descEdit->text();
    if(desc.isEmpty()){
        return;
    }
    // 2. 发送网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::changeDescriptionDone, this, &SelfInfoWidget::chickDescSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changeDescriptionAsync(desc);
}
void SelfInfoWidget::chickDescSubmitBtnDone(){
    descLabel->setText(descEdit->text());
    descStack->setCurrentIndex(0);
    descBtnStack->setCurrentIndex(0);
}

/// 修改手机号
void SelfInfoWidget::clickGetVerifyCodeBtn(){
    // 1. 获取输入框验证码
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty()){
        return;
    }
    // 2. 发起请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getVerifyCodeDone, this, [=]() {
        // 不需要做其他的处理, 只需要提示一下, 验证码已经发送
        Toast::showMessage("短信验证码已经发送");
    });
    dataCenter->getVerifyCodeAsync(phone);

    // 3. 保存发送请求的手机号码
    this->phoneToChange = phone; // 后续点击提交按钮, 修改电话, 不从输入框读取, 而是读取这个变量.

    // 4. 禁用发送验证码按钮, 并给出倒计时
    this->getVerifyCodeBtn->setEnabled(false);

    leftTime = 30;
    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=]() {
        if (leftTime <= 1) {
            // 倒计时结束了
            getVerifyCodeBtn->setEnabled(true);
            getVerifyCodeBtn->setText("获取验证码");
            timer->stop();
            timer->deleteLater();
            return;
        }
        --leftTime;
        getVerifyCodeBtn->setText(QString::number(leftTime) + "s");
    });
    timer->start(1000);
}

void SelfInfoWidget::clickPhoneSubmitBtn(){
    // 1. 判断验证码是否收到
    DataCenter* dataCenter = DataCenter::getInstance();
    QString verifyCodeId = dataCenter->getVerifyCodeId();
    if(verifyCodeId.isEmpty()){
        // 服务器这边还没有返回验证码响应呢
        // LOG() << "服务器尚未返回验证码! 稍后重试!";
        Toast::showMessage("服务器尚未返回响应, 稍后重试!");
        return;
    }

    dataCenter->resetVerifyCodeId(""); // 清空原有值

    // 2. 获取用户输入
    QString verifyCode = verifyCodeEdit->text();
    if (verifyCode.isEmpty()) {
        Toast::showMessage("验证码不能为空!");
        return;
    }
    verifyCodeEdit->setText("");  // 获取到验证码之后, 就可以清空了.

    // 3. 发送请求, 把当前验证码信息, 发送给服务器
    connect(dataCenter, &DataCenter::changePhoneDone, this, &SelfInfoWidget::clickPhoneSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changePhoneAsync(this->phoneToChange, verifyCodeId, verifyCode);

    // 4. 让验证码按钮的倒计时停止. 把 leftTime 设为 1, 就可以停止了
    leftTime = 1;
}

void SelfInfoWidget::clickPhoneSubmitBtnDone(){
    phoneLabel->setText(phoneEdit->text());
    phoneStack->setCurrentIndex(0);
    phoneBtnStack->setCurrentIndex(0);
    verifyStack->setCurrentIndex(0); // 隐藏验证码行
}

/// 修改头像
void SelfInfoWidget::clickAvatarBtn(){
    // 1. 弹出对话框 选中文件
    QString filter = "Image Files (*.png *.jpg *.jpeg)";
    QString imagePath = QFileDialog::getOpenFileName(this, "选择头像", QDir::homePath(), filter);
    if(imagePath.isEmpty()){
        LOG() << "用户未选择任何头像";
        return;
    }

    // 2. 根据路径读取文件内容
    QByteArray imageBytes = loadFileToByteArray(imagePath);

    // 3. 发送请求修改头像
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::changeAvatarDone, this, &SelfInfoWidget::clickAvatarBtnDone, Qt::UniqueConnection);
    dataCenter->changeAvatarAsync(imageBytes);
}
void SelfInfoWidget::clickAvatarBtnDone(){
    // 更新头像
    DataCenter* dataCenter = DataCenter::getInstance();
    avatarBtn->setIcon(dataCenter->getMyself()->avatar);
}


// ------------------------------------
// ------ QStackedWidget + Qt::Popup 解决问题 ----
// ------------------------------------
// SelfInfoWidget::SelfInfoWidget(QWidget* parent): QDialog(parent) {
//     // 设置对象名，方便QSS定位
//     this->setObjectName("SelfInfoDialog");
//     // 1. 设置属性和窗口标志
//     this->setFixedSize(500, 230);
//     this->setAttribute(Qt::WA_DeleteOnClose);
//     this->setAttribute(Qt::WA_TranslucentBackground);
//     this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);
//     this->move(QCursor::pos());

//     // 2. 创建布局管理器
//     QWidget* centralWidget = new QWidget(this);
//     centralWidget->setObjectName("SelfInfoDialog");
//     centralWidget->setStyleSheet(
//         "#SelfInfoDialog {"
//         "background-color: #f8f8f8;"
//         "border-radius: 10px;"
//         "}"
//         );
//     // 阴影布局
//     QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
//     effect->setOffset(0, 0);
//     effect->setColor(QColor(100, 100, 100, 150));
//     effect->setBlurRadius(8);
//     centralWidget->setGraphicsEffect(effect);

//     // 设置 QDialog 的主布局
//     QVBoxLayout* dialogLayout = new QVBoxLayout(this);
//     dialogLayout->setContentsMargins(2, 2, 2, 2);
//     dialogLayout->addWidget(centralWidget); // 将 centralWidget 添加到 QDialog 的主布局中

//     // 为 centralWidget 设置内部布局
//     layout = new QGridLayout(centralWidget); // 将布局应用到 centralWidget
//     layout->setSpacing(5);
//     layout->setContentsMargins(10, 20, 10, 10);
//     layout->setHorizontalSpacing(10);   // 行宽
//     layout->setAlignment(Qt::AlignTop);

//     // 3. 定义 QSS 样式
//     QString baseFont = "font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;";
//     QString labelStyle = QString(R"(
//         QLabel {
//             %1
//             font-size: 15px;
//             font-weight: 500;
//             color: #555;
//         }
//     )").arg(baseFont);
//     QString btnStyle = QString(R"(
//         QPushButton{
//             %1
//             font-size: 13px;
//             font-weight: 500;
//             color: #555;
//             border: 1px solid #ccc;
//             background-color: #e8e8e8;
//             border-radius: 6px;
//             padding: 3px 8px;
//         }
//         QPushButton:hover{
//             background-color: #e0e0e0;
//             border-color: #bbb;
//         }
//         QPushButton:pressed{
//             color: #ffffff;
//             background-color: #07c160;
//             border-color: #07c160;
//         }
//     )").arg(baseFont);
//     QString editStyle = QString(R"(
//         QLineEdit {
//             %1
//             border: 1px solid #e0e0e0;
//             border-radius: 6px;
//             padding: 4px 8px;
//             background-color: #ffffff;
//             color: #333;
//             font-size: 14px;
//         }
//         QLineEdit:focus {
//             border: 1px solid #07c160;
//         }
//     )").arg(baseFont);
//     QString infoLabelStyle = QString(R"(
//         QLabel {
//             %1
//             font-size: 14px;
//             color: #333;
//             background-color: rgb(225, 225, 225);
//             border: 1px solid #f0f0f0;
//             border-radius: 6px;
//             padding: 4px 8px;
//         }
//     )").arg(baseFont);

//     int rowWidth = 50, rowHeight = 35;
//     int btnWidth = 75, btnHeight = 28;

//     // 4. 创建头像
//     avatarBtn = new QPushButton();
//     avatarBtn->setObjectName("avatarButton");
//     avatarBtn->setFixedSize(80, 80);
//     avatarBtn->setIconSize(QSize(80, 80));
//     avatarBtn->setStyleSheet(R"(
//         #avatarButton {
//             border: none;
//             border-radius: 40px;
//             background-color: transparent;
//         }
//     )");

//     // 5. id 信息 (只读)
//     idTag = new QLabel("序号");
//     idTag->setFixedSize(rowWidth, rowHeight);
//     idTag->setStyleSheet(labelStyle);
//     idLabel = new QLabel();
//     idLabel->setFixedHeight(rowHeight);
//     idLabel->setStyleSheet(infoLabelStyle);
//     idLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);

//     // 6. 昵称信息
//     nameTag = new QLabel("昵称");
//     nameTag->setFixedSize(50, rowHeight);
//     nameTag->setStyleSheet(labelStyle);

//     // 创建昵称的 QStackedWidget
//     nameStack = new QStackedWidget();
//     nameLabel = new QLabel();
//     nameLabel->setFixedHeight(rowHeight);
//     nameLabel->setStyleSheet(infoLabelStyle);
//     nameLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
//     nameEdit = new QLineEdit();
//     nameEdit->setFixedHeight(rowHeight);
//     nameEdit->setStyleSheet(editStyle);
//     nameStack->addWidget(nameLabel); // 索引 0: 显示
//     nameStack->addWidget(nameEdit);  // 索引 1: 编辑
//     nameStack->setCurrentIndex(0);   // 初始显示 QLabel
//     // nameStack->setFixedHeight(rowHeight);

//     nameModifyBtn = new QPushButton("修改");
//     nameModifyBtn->setFixedSize(btnWidth, btnHeight);
//     nameModifyBtn->setStyleSheet(btnStyle);
//     nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
//     nameModifyBtn->setIconSize(QSize(16, 16));
//     nameSubmitBtn = new QPushButton("提交");
//     nameSubmitBtn->setFixedSize(btnWidth, btnHeight);
//     nameSubmitBtn->setStyleSheet(btnStyle);
//     nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
//     nameSubmitBtn->setIconSize(QSize(16, 16));
//     nameBtnStack = new QStackedWidget();
//     nameBtnStack->addWidget(nameModifyBtn); // 索引 0: 修改按钮
//     nameBtnStack->addWidget(nameSubmitBtn); // 索引 1: 提交按钮
//     nameBtnStack->setCurrentIndex(0);       // 初始显示修改按钮

//     // 7. 签名信息
//     descTag = new QLabel("签名");
//     descTag->setFixedSize(rowWidth, rowHeight);
//     descTag->setStyleSheet(labelStyle);

//     // 创建签名的 QStackedWidget
//     descStack = new QStackedWidget();
//     descLabel = new QLabel();
//     descLabel->setFixedHeight(rowHeight);
//     descLabel->setStyleSheet(infoLabelStyle);
//     descLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
//     descEdit = new QLineEdit();
//     descEdit->setFixedHeight(rowHeight);
//     descEdit->setStyleSheet(editStyle);
//     descEdit->setMaxLength(30);

//     descStack->addWidget(descLabel); // 索引 0: 显示
//     descStack->addWidget(descEdit);  // 索引 1: 编辑
//     descStack->setCurrentIndex(0);   // 初始显示 QLabel
//     // descStack->setFixedHeight(rowHeight);

//     descModifyBtn = new QPushButton("修改");
//     descModifyBtn->setFixedSize(btnWidth, btnHeight);
//     descModifyBtn->setStyleSheet(btnStyle);
//     descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
//     descModifyBtn->setIconSize(QSize(16, 16));
//     descSubmitBtn = new QPushButton("提交");
//     descSubmitBtn->setFixedSize(btnWidth, btnHeight);
//     descSubmitBtn->setStyleSheet(btnStyle);
//     descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
//     descSubmitBtn->setIconSize(QSize(16, 16));

//     descBtnStack = new QStackedWidget();
//     descBtnStack->addWidget(descModifyBtn);
//     descBtnStack->addWidget(descSubmitBtn);
//     descBtnStack->setCurrentIndex(0);


//     // 8. 电话信息
//     phoneTag = new QLabel("电话");
//     phoneTag->setFixedSize(rowWidth, rowHeight);
//     phoneTag->setStyleSheet(labelStyle);

//     // 创建电话的 QStackedWidget
//     phoneStack = new QStackedWidget();
//     phoneLabel = new QLabel();
//     phoneLabel->setFixedHeight(rowHeight);
//     phoneLabel->setStyleSheet(infoLabelStyle);
//     phoneLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
//     phoneEdit = new QLineEdit();
//     phoneEdit->setFixedHeight(rowHeight);
//     phoneEdit->setStyleSheet(editStyle);
//     phoneStack->addWidget(phoneLabel); // 索引 0: 显示
//     phoneStack->addWidget(phoneEdit);  // 索引 1: 编辑
//     phoneStack->setCurrentIndex(0);    // 初始显示 QLabel
//     // phoneStack->setFixedHeight(rowHeight);

//     phoneModifyBtn = new QPushButton("修改");
//     phoneModifyBtn->setFixedSize(btnWidth, btnHeight);
//     phoneModifyBtn->setStyleSheet(btnStyle);
//     phoneModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
//     phoneModifyBtn->setIconSize(QSize(16, 16));
//     phoneSubmitBtn = new QPushButton("提交");
//     phoneSubmitBtn->setFixedSize(btnWidth, btnHeight);
//     phoneSubmitBtn->setStyleSheet(btnStyle);
//     phoneSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
//     phoneSubmitBtn->setIconSize(QSize(16, 16));
//     phoneBtnStack = new QStackedWidget();
//     phoneBtnStack->addWidget(phoneModifyBtn);
//     phoneBtnStack->addWidget(phoneSubmitBtn);
//     phoneBtnStack->setCurrentIndex(0);

//     // 9. 验证码信息
//     // 创建验证码的 QStackedWidget，用于控制整行的显示/隐藏
//     verifyStack = new QStackedWidget();
//     verifyCodeTag = new QLabel("验证码");
//     verifyCodeTag->setFixedSize(rowWidth, rowHeight);
//     verifyCodeTag->setStyleSheet(labelStyle);
//     verifyCodeEdit = new QLineEdit();
//     verifyCodeEdit->setFixedHeight(rowHeight);
//     verifyCodeEdit->setStyleSheet(editStyle);
//     getVerifyCodeBtn = new QPushButton("获取验证码");
//     getVerifyCodeBtn->setStyleSheet(btnStyle);
//     getVerifyCodeBtn->setFixedSize(btnWidth + 20, btnHeight);

//     // 将验证码控件放入一个 QWidget 容器
//     QWidget* verifyWidget = new QWidget();
//     QHBoxLayout* verifyLayout = new QHBoxLayout(verifyWidget);
//     verifyLayout->setContentsMargins(0, 0, 0, 0);
//     verifyLayout->setSpacing(5);
//     verifyLayout->addWidget(verifyCodeTag);
//     verifyLayout->addWidget(verifyCodeEdit, 1);
//     verifyLayout->addWidget(getVerifyCodeBtn);

//     // 将容器和一个空 widget 添加到 QStackedWidget
//     verifyStack->addWidget(new QWidget()); // 索引 0: 空（隐藏）
//     verifyStack->addWidget(verifyWidget);  // 索引 1: 显示验证码行
//     verifyStack->setCurrentIndex(0);       // 初始隐藏

    // // 10. 添加到布局管理器
    // layout->addWidget(avatarBtn, 0, 0, 4, 1, Qt::AlignCenter);

    // // 序号
    // layout->addWidget(idTag, 0, 1, Qt::AlignVCenter | Qt::AlignRight);
    // layout->addWidget(idLabel, 0, 2, 1, 2);

    // // 昵称
    // layout->addWidget(nameTag, 1, 1, Qt::AlignVCenter | Qt::AlignRight);
    // layout->addWidget(nameStack, 1, 2, Qt::AlignVCenter);
    // layout->addWidget(nameBtnStack, 1, 3, Qt::AlignVCenter);

    // // 签名
    // layout->addWidget(descTag, 2, 1, Qt::AlignVCenter | Qt::AlignRight);
    // layout->addWidget(descStack, 2, 2, Qt::AlignVCenter);
    // layout->addWidget(descBtnStack, 2, 3, Qt::AlignVCenter);

    // // 电话
    // layout->addWidget(phoneTag, 3, 1, Qt::AlignVCenter | Qt::AlignRight);
    // layout->addWidget(phoneStack, 3, 2, Qt::AlignVCenter);
    // layout->addWidget(phoneBtnStack, 3, 3, Qt::AlignVCenter);

    // // 验证码
    // layout->addWidget(verifyStack, 4, 1, 1, 3, Qt::AlignVCenter); // 跨三列

//     // 设置列的伸展因子
//     layout->setColumnStretch(0, 0);
//     layout->setColumnMinimumWidth(0, 80);
//     layout->setColumnStretch(1, 0);
//     layout->setColumnMinimumWidth(1, 50);
//     layout->setColumnStretch(2, 1);
//     layout->setColumnStretch(3, 0);
//     layout->setColumnMinimumWidth(3, btnWidth);

// #if TEST_UI
//     // 加载并裁剪头像
//     QPixmap srcAvatar(":/resource/image/kenan.png");
//     if (!srcAvatar.isNull()) {
//         QPixmap roundedAvatar(80, 80);
//         roundedAvatar.fill(Qt::transparent);
//         QPainter avatarPainter(&roundedAvatar);
//         avatarPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
//         QPainterPath avatarPath;
//         avatarPath.addRoundedRect(roundedAvatar.rect(), 40, 40);
//         avatarPainter.setClipPath(avatarPath);
//         avatarPainter.drawPixmap(0, 0, 80, 80, srcAvatar.scaled(80, 80, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
//         avatarPainter.end();
//         avatarBtn->setIcon(QIcon(roundedAvatar));
//     } else {
//         qDebug() << "Failed to load avatar image: kenan.png";
//     }
//     idLabel->setText("123456789");
//     nameLabel->setText("张三丰");
//     descLabel->setText("从今天开始认真敲代码!");
//     phoneLabel->setText("186 1234 5678");
// #endif

//     // 11. 加载信号槽
//     initSingalSlot();
// }

// void SelfInfoWidget::initSingalSlot()
// {
//     connect(nameModifyBtn, &QPushButton::clicked, this, [this]() {
//         nameBtnStack->setCurrentIndex(1); // 切换到提交按钮
//         nameStack->setCurrentIndex(1);    // 切换到编辑框
//         nameEdit->setFocus();
//         nameEdit->setText(nameLabel->text());
//     });
//     connect(nameSubmitBtn, &QPushButton::clicked, this, [this]() {
//         nameLabel->setText(nameEdit->text());
//         nameStack->setCurrentIndex(0); // 切换回显示
//         nameBtnStack->setCurrentIndex(0); // 切换回修改按钮
//     });

//     // 签名修改
//     connect(descModifyBtn, &QPushButton::clicked, this, [this]() {
//         descBtnStack->setCurrentIndex(1);
//         descStack->setCurrentIndex(1);
//         descEdit->setFocus();
//         descEdit->setText(descLabel->text());
//     });
//     connect(descSubmitBtn, &QPushButton::clicked, this, [this]() {
//         descLabel->setText(descEdit->text());
//         descStack->setCurrentIndex(0);
//         descBtnStack->setCurrentIndex(0);
//     });

//     // 电话修改
//     connect(phoneModifyBtn, &QPushButton::clicked, this, [this]() {
//         phoneBtnStack->setCurrentIndex(1);
//         phoneStack->setCurrentIndex(1);
//         phoneEdit->setFocus();
//         phoneEdit->setText(phoneLabel->text());
//         verifyStack->setCurrentIndex(1); // 显示验证码行
//     });
//     connect(phoneSubmitBtn, &QPushButton::clicked, this, [this]() {
//         phoneLabel->setText(phoneEdit->text());
//         phoneStack->setCurrentIndex(0);
//         phoneBtnStack->setCurrentIndex(0);
//         verifyStack->setCurrentIndex(0); // 隐藏验证码行
//     });

//     // 头像点击
//     connect(avatarBtn, &QPushButton::clicked, this, []() {
//         qDebug() << "点击头像";
//     });
// }















































































































